import React from "react";

class UseBind extends React.Component {

    // 第二种初始化方法
    state = {
        count: 1
    };

    constructor() {
        super();
        // 通过bind方法改变了当前函数中this的指向
        this.onIncrement = this.onIncrement.bind(this)
    }

    onIncrement = () => {
        console.log('事件处理程序中的this：', this)
        this.setState({
            count: this.state.count + 1
        })
    }
    render() {
        return (
            <div>
                <div>计数器 :{this.state.count}</div>
                <button onClick={() => {this.onIncrement()}}>+1</button>
            </div>
        )
    }
}

export default UseBind;